<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员等级
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$merchid=$this->checklogin();
$tabs=array(
	array('op'=>'list','title'=>'等级列表'),
	array('op'=>'edit','title'=>'添加等级'),
);
$return['up_type']=array(0=>'订单完成',1=>'订单支付',2=>'付费开卡');
$return['orderstar']=array('0'=>'订单不加星','1'=>'订单VIP加星');
//等级列表
if($_RQ['op']=='list'){
	$params=array('merchid'=>$merchid);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','level','title','orderstar','ordermoney','ordercount','discount','status','isup','up_type','merchid');
	$list=pdo_getslice('jy_weishop_member_level',$params,array($pindex,$psize),$total,$fields,'','level asc,id desc');
	if($list){
		foreach($list as &$item){      
		    $item['h5url']=custom_url('core/member.hyset//merchid='.$item['merchid'].'&levelid='.$item['id'],true);
		    $item['h5sorturl'] = 'core/member.hyset//merchid='.$item['merchid'].'&levelid='.$item['id'];
		}
	}

	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'开启自动升级，或者选择付费开卡，前端才会显示升级会员优惠的提示！','edit'=>false,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'level','title'=>'等级权重','type'=>'string'),
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('type'=>'radio','title'=>'订单加星?','name'=>'orderstar','options'=>'orderstar'),
			array('type'=>'radio','title'=>'升级类型','name'=>'up_type','options'=>'up_type'),
			array('name'=>'discount','title'=>'折扣','type'=>'string'),
			array('name'=>'ordermoney','title'=>'升级条件（订单金额满）','type'=>'string'),
			array('name'=>'ordercount','title'=>'升级条件（订单数量满）','type'=>'string'),
			array('name'=>'h5url','title'=>'h5办理会员二维码','type'=>'ewm'),
			array('name'=>'h5sorturl','title'=>'复制短链接','type'=>'copy'),
			array('name'=>'h5url','title'=>'复制长链接','type'=>'copy'),
			array('type'=>'post','op'=>'download','title'=>'下载小程序码'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'action'=>array(
			array('op'=>'edit','title'=>'编辑','type'=>'href','show'=>array('level >'=>0)),
			array('op'=>'scewm','title'=>'生成小程序码','type'=>'post'),

		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'等级名称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status2'),
		)
	);
	$return['status']=array('0'=>'关闭','1'=>'启用');
	$return['status2']=array(array('id'=>0,'title'=>'关闭'),array('id'=>1,'title'=>'启用'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_member_level',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$sheng_fees = array(
			array('title'=>'VIP折扣','fee'=>'980'),
			array('title'=>'优惠券','fee'=>'680'),
			array('title'=>'专享商品','fee'=>'580'),
	);
	$item['vipimgs']=$item['vipimgs']?:'../addons/jy_weishop/resource/images/vipimgs.png';
	$item['cardbg']=$item['cardbg']?:'../core/resource/images/cardbg.png';
	$item['goods_ids']=$item['goods_ids']?json_decode($item['goods_ids'],true):array();
	$item['sheng_fees']=$item['sheng_fees']?json_decode($item['sheng_fees'],true):$sheng_fees;
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回等级列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'select','title'=>'等级权重','name'=>'level','options'=>'levels','option_value'=>'level','help'=>'权重数字越大等级越高，等级权重必须独立，不能跟其他等级的权重一样！'),
					array('type'=>'string','title'=>'等级名称','name'=>'title','help'=>''),
					array('type'=>'string','title'=>'等级描述','name'=>'desc'),
					array('type'=>'radio','title'=>'订单加星?','name'=>'orderstar','options'=>'orderstar'),
					array('type'=>'image','title'=>'等级权益图','name'=>'vipimgs'),
					array('type'=>'image','title'=>'等级背景图','name'=>'cardbg'),
					array('type'=>'image','title'=>'等级权益说明','name'=>'vipcontent'),
					//array('type'=>'switch','title'=>'是否开启转化虚拟币操作','name'=>'isxunnizh','help'=>'开启后设置成为该等级时,该会员名下所有优惠劵等值转换为虚拟币,优惠劵全部失效！'),
					array('type'=>'switch','title'=>'是否开启自动升级','name'=>'isup','help'=>'开启自动升级，用户触发下面的<red>所有条件</red>，才会自动升级会员；或者付费开卡才行！'),
					array('type'=>'radio','title'=>'升级类型','name'=>'up_type','options'=>'up_type','help'=>'《付费开卡》表示必须付费开卡才行！'),
					array('type'=>'unitstring','unit'=>'元','title'=>'开卡年费','name'=>'open_fee','show'=>array('up_type'=>2)),
					array('type'=>'choose_many','title'=>'购买指定商品升级','name'=>'goods_ids','show'=>array('up_type'=>array(0,1)),'action'=>'jy_merch/manage.get_goods/many/nopluginname=1','choose_title'=>'选择参与的商品','help'=>'（多个商品时，任意其中一个）注意：注意触发升级时机，需核销的商品，只有核销后才会完成！虚拟商品，发货后自动会完成！'),
					array('type'=>'string','title'=>'升级条件（订单金额满）','name'=>'ordermoney','show'=>array('up_type'=>array(0,1)),'help'=>'注意触发升级时机，注意：后台手动强制修改订单状态，不会触发！'),
					array('type'=>'string','title'=>'升级条件（订单数量满）','name'=>'ordercount','show'=>array('up_type'=>array(0,1)),'help'=>'注意触发升级时机，注意：后台手动强制修改完成状态，不会触发！'),
					array('type'=>'string','title'=>'折扣(比)','name'=>'discount','help'=>'请输入0.1~10之间的数字,值为空代表不设置折扣；例如8，是指8折'),
					array('type'=>'checkbox','name'=>'couponids','title'=>'升级赠送优惠券','options'=>'coupons'),
					array('type'=>'tip','title'=>'[温馨提示]：VIP等级特权商品，请移步：具体的商品--编辑--定价/购买/权限'),
					array('type'=>'array','title'=>'年省钱明细','name'=>'sheng_fees','add_tip'=>'添加一个省钱明细',
							'list'=>array(
										array('type'=>'string','name'=>'title','title'=>'标题'),
										array('type'=>'string','name'=>'fee','title'=>'省钱额'),
							),
									'details'=>array(
										array('type'=>'string','name'=>'title','title'=>'标题'),
										array('type'=>'string','name'=>'fee','title'=>'省钱额'),
							)
					),
					array('type'=>'switch','title'=>'支持提现红包？','name'=>'can_tred','help'=>'这里的提现红包是商城会员本身的提现红包（比如多商户的现金红包返现），不是系统的现金红包！'),
					array('type'=>'switch','title'=>'状态','name'=>'status','help'=>''),

				)
			),
		)
	);
	$coupon=pdo_getall('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','title'));
	$return['coupons']=array();
	foreach($coupon as $v){
		$id=$v['id'];
		$return['coupons'][$id]=$v['title'];
	}
	
	$levels=array();
	for($i=1;$i<=100;$i++){
		$levels[]=array('level'=>$i,'title'=>$i.'级');
	}
	$return['levels']=$levels;
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='download'){
    $id=intval($_RQ['id']);
   
    $file=pdo_getcolumn('jy_weishop_member_level',array('id'=>$id),'wxappurl');
    $file=tomedia($file);
    exi('','','',$file);
}

if($_RQ['op']=='scewm'){
	$id=intval($_RQ['id']);

	if(!$id){
		exi('请先选择会员等级！','error');
	}
	$url='/core/member.hyset//id='.$id;
    $pid='levelid'.$id.'bl'.intval($_SESSION['uid']);
    $res = model('jy_weishop/model')->htget_wxappewm($url,$pid);
	if($res){
	    pdo_update('jy_weishop_member_level',array('wxappurl'=>'oss/'.$_SESSION['uniacid'].'/images/jy_weishop/'.$pid.'jpg'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id,'merchid'=>$merchid));
	   exi('生成小程序码成功！');
	}else{
	   exi('生成小程序码失败！');
	}
	
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['level']){
		exi('等级权重必选！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'desc'=>trim($postdata['desc']),
		'level'=>intval($postdata['level']),
		'isup'=>intval($postdata['isup']),
		//'isxunnizh'=>intval($postdata['isxunnizh']),
		'up_type'=>intval($postdata['up_type']),
		'ordercount'=>intval($postdata['ordercount']),
		'ordermoney'=>round($postdata['ordermoney'],2),
		'discount'=>round($postdata['discount'],2),
		'status'=>intval($postdata['status']),
		'open_fee'=>trim($postdata['open_fee']),
		'cardbg'=>trim($postdata['cardbg']),
		'vipimgs'=>trim($postdata['vipimgs']),
		//'vipcontent'=>trim($postdata['vipcontent']),
		'goods_ids'=>ijson_encode($postdata['goods_ids']),
		'sheng_fees'=>ijson_encode($postdata['sheng_fees']),
		'couponids'=>trim($postdata['couponids']),
		'can_tred'=>intval($postdata['can_tred']),
		'orderstar'=>intval($postdata['orderstar']),
	);
	$isadd=pdo_get('jy_weishop_member_level',array('level'=>intval($postdata['level']),'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid));
	$id=intval($postdata['id']);
	if($id){
		if($id != $isadd['id'] && $isadd['id']){
			exi('该等级权重已经添加了，请换一个！','error');
		}
		pdo_update('jy_weishop_member_level',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		if($isadd['id']){
			exi('该等级权重已经添加了，请换一个！','error');
		}
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$merchid;
		pdo_insert('jy_weishop_member_level',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	if(!$del){
		exi('默认等级不能删除！','error');
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			if(!$id){
				exi('默认等级不能删除！','error');
			}
			$issetid=pdo_getcolumn('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'level'=>$id),'id');
			if($issetid){
				exi('该会员等级下面已经有会员，不能删除！','error');
			}
			$issetid=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'showlevels like'=>'%'.$id.'%'),'id');
			if($issetid){
				exi('该会员等级下面已经有商品绑定，不能删除！','error');
			}
			pdo_delete('jy_weishop_member_level',array('id'=>$id,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('默认等级不能操作！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_weishop_member_level',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid));
	}
	exi('操作成功！');
}